<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList()">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="getUserList()">查询</el-button>
        </el-col>
        <el-col :span="4">
          <el-button type="success" @click="isShowAddUserDialog=true">添加用户</el-button>
        </el-col>
      </el-row>

      <el-table :data="userList" border stripe style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{scope.row.create_time | dateFormatFun(4)}}</template>
        </el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="userStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
            <!-- 分配角色按钮 -->
            <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="setRole(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--  
        @size-change="handleSizeChange" 点击每页显示多少条时候触发
        @current-change="handleCurrentChange" 点击第几页时候触发
        :current-page="currentPage4" 显示当前第几页
        :page-sizes="[100, 200, 300, 400]" 下拉一共有多少选项
        :page-size="100" 当前选择每页显示多少条
        layout="total, sizes, prev, pager, next, jumper" 显示的组件
        :total="400" 一共多少条数据
      -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <!-- 向子组件传一个名为isShow值为isShowAddUserDialog -->
    <addUserDialog :isShow="isShowAddUserDialog" @setIsShow="setAddIsShow" @Refresh="getUserList"></addUserDialog>
    <editUserDialog
      :userid="editId"
      :isShow="isShowEditUserDialog"
      @closeShow="isShowEditUserDialog=false"
      @Refresh="getUserList"
    ></editUserDialog>
  </div>
</template>

<script>
//引入添加用户的模板
import addUserDialog from './addUserDialog.vue'
import editUserDialog from './editUserDialog.vue'
export default {
  components: {
    addUserDialog,
    editUserDialog
  },
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      userList: [],
      total: 0,
      isShowAddUserDialog: false,
      isShowEditUserDialog: false,
      //当前要修改的用户
      editId: ''
    }
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get('users', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        this.userList = res.data.users
        this.total = res.data.total
      }
    },
    //修改用户信息
    showEditDialog(id) {
      this.editId = id
      this.isShowEditUserDialog = true
    },
    //删除用户
    removeUserById(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete('users/' + id)
          if (res.meta.status === 200) {
            this.$message.success('删除成功!')
            this.getUserList()
          } else {
            this.$message.success(res.meta.msg)
          }
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    setRole(row) {},
    //监听最新每页显示多少条
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    //监听点击第几页  页码值
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getUserList()
    },
    //修改用户状态
    async userStatusChange(userInfo) {
      const { data: res } = await this.$http.put(
        `users/${userInfo.id}/state/${userInfo.mg_state}`
      )
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        //失败之后将按钮重置为未修改的状态
        userInfo.mg_state = !userInfo.mg_state
      } else {
        this.$message.success('保存成功！')
      }
    },
    setAddIsShow(isShow) {
      this.isShowAddUserDialog = isShow
    }
  },
  created() {
    this.getUserList()
  }
}
</script>

<style lang="less" scoped>
</style>